﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebChat.Models;
using System.Web.Caching;
using System.Text.RegularExpressions;

namespace WebChat.Controllers
{
    /// <summary>
    /// User Controller, tar seg av alt som har med brukere og gjøre
    /// Dette er for Admin brukere som da kan slette vanlige brukere
    /// eller endre dem.
    /// </summary>
    public class UserController : Controller
    {

        private UserRepository userRep;
        private MessageRepository messageRep;
        Regex telefonnr = new Regex("^[0-9]{1,20}$");
        /// <summary>
        /// Konstruktør
        /// </summary>
        public UserController()
        {
            userRep = new UserRepository();
            messageRep = new MessageRepository();
        }

        /// <summary>
        /// GET: /User/
        /// </summary>
        /// <returns>Index viewet</returns>
        [Authorize]
        public ActionResult Index()
        {
            List<aspnet_User> users = userRep.showAllUsers().ToList();
            return View(users);
        }

        /// <summary>
        /// GET: /User/Details/id
        /// Returnerer den brukeren du vil ha
        /// </summary>
        /// <param name="id">brukerens id</param>
        [Authorize]
        public ActionResult Details(Guid id)
        {
            aspnet_User User = userRep.showUser(id);
            if (User == null) 
                return View("NotFound");
            return View(User);
        }

        /// <summary>
        /// GET: /User/Edit/id
        /// Sender deg til ett endre skjema
        /// </summary>
        /// <param name="id">Brukerens id</param>
        /// <returns></returns>
        [Authorize]
        public ActionResult EditUser()
        {
            Guid id = userRep.getUserId(User.Identity.Name);
            aspnet_User user = userRep.showUser(id);
            if (user == null)
                return View("NotFound");
            return View(user);
        }

        /// <summary>
        /// POST: /User/Edit/id
        /// Sender inn endre skjemaet
        /// </summary>
        /// <param name="id">Brukerens id</param>
        /// <param name="collection"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public ActionResult EditUser(FormCollection collection)
        {
            Image image = new Image();
            HttpPostedFileBase file;
            Guid id = userRep.getUserId(User.Identity.Name);

            try
            {
                aspnet_User user = userRep.showUser(id);
                UpdateModel(user);

                if (user.MobilePhoneNumber != null && !telefonnr.IsMatch(user.MobilePhoneNumber))
                {
                    ModelState.AddModelError("MobilePhoneNumber", "Dette feltet kan ha maks 20 tegn og må bestå av bare tall");
                    return View(user);
                }

                int imgId = user.imageId;

                file = Request.Files[0];

                //Logikk for å få bilde inn i databasen
                if (file.ContentLength > 0)
                {
                    if (file.ContentLength > 1000000)
                    {
                        ModelState.AddModelError("imageId", "Filen er for stor.");
                        return View(user);
                    }
                    if (file.ContentType != "image/jpeg" && file.ContentType != "image/png" && file.ContentType != "image/gif")
                    {
                        ModelState.AddModelError("imageId", "Feil filtype. Støtter .jpg, .png og .gif");
                        return View(user);
                    }
                    image.imageName = file.FileName;
                    image.imageSize = file.ContentLength;
                    image.imageType = file.ContentType;
                    byte[] theImage = new byte[file.ContentLength];
                    file.InputStream.Read(theImage, 0, (int)file.ContentLength);
                    image.image1 = theImage;
                    user.imageId = messageRep.addImage(image);
                }

                userRep.updateUser(user);
                return RedirectToAction("../Home");
            }
            catch (Exception e)
            {
                return View();
            }
        }
        


        /// <summary>
        /// GET: /User/Delete/id
        /// Lar deg slette en bruker
        /// </summary>
        /// <param name="id">Brukerens id</param>
        /// <returns></returns>
        [Authorize]
        public ActionResult Delete(Guid id)
        {
                aspnet_User User = userRep.showUser(id);
                if (User == null)
                    return View("NotFound");
                userRep.deleteUser(User);
 
                return RedirectToAction("Index");
        }
    }
}
